<template>
  <el-dialog title="分配权限" :visible="value" @close="close">
    <el-tree ref="treeRef" :data="data" :props="defaultProps" node-key="id" show-checkbox :default-checked-keys="permIds" :default-expanded-keys="permIds"></el-tree>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close()">取 消</el-button>
      <el-button type="primary" @click="submitPremission">确 定</el-button>
    </span>
  </el-dialog>
</template>
<script>
import { getPermissionListRequest, } from "@/api/permission";
import { transListToTreeData } from '@/utils'
import { assignPremRequest, getRolesListRequest } from "@/api/role";
export default {
  name: '',
  props: {
    value: {
      type: Boolean,
      default: false
    },
    currentId: {
      type: Number,
      default: null
    },
    permIds: {
      type: Array
    }
  },
  data () {
    return {
      data: [],
      defaultProps: {
        children: 'children',
        label: 'name'
      },
      permIds: []
    }
  },
  created () {
    this.getPermissionList()
  },
  computed: {
  },
  methods: {
    async getPermissionList () {
      const res = await getPermissionListRequest()
      console.log(res)
      //平铺数据转换为树形数据
      this.data = transListToTreeData(res.data, 0)
    },
    async submitPremission () {
      this.permIds = this.$refs.treeRef.getCheckedKeys()

      await assignPremRequest({ id: this.currentId, permIds: this.permIds })
      this.close()
    },
    // async getRolesList (id) {
    //   const res = await getRolesListRequest(id)
    //   console.log(res)

    //   this.permIds = res.data.permIds
    // },
    close () {
      this.$emit('input', false)
    }
  }
}
</script>
<style lang='less'  scoped>
</style>
